package com.fancy.learncenter.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.DeBindSchoolDataBean;
import com.fancy.learncenter.bean.MySchoolDetailDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.activity.base.MallCommonH5Activity;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class MySchoolActivity extends BaseActivity {

    @Bind(R.id.school_loction)
    TextView schoolLoction;
    @Bind(R.id.cancel_school)
    TextView cancelSchool;
    @Bind(R.id.school_bind)
    TextView schoolBind;


    long schoolId;
    @Bind(R.id.my_course_next)
    CardView myCourseNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_school, Status.STATUS, "我的学校");
        ButterKnife.bind(this);

        Constant.isBind = true;
        getSchoolDeatail();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    /**
     * 回调
     *
     * @param intent
     */
    @Override
    protected void onNewIntent(Intent intent) {

        String schoolName = intent.getStringExtra("school_name");
        long passSchoolId = intent.getLongExtra("school_id", 0);
        if (!TextUtils.isEmpty(schoolName)) {
            schoolBind.setVisibility(View.GONE);
            cancelSchool.setVisibility(View.VISIBLE);
            schoolId = passSchoolId;
            schoolLoction.setText(schoolName);
        }

        super.onNewIntent(intent);
    }

    private void getSchoolDeatail() {
        HttpMehtod.getInstance().getMySchoolDeatail(new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<MySchoolDetailDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), MySchoolDetailDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<MySchoolDetailDataBean>() {
                    @Override
                    public void success(MySchoolDetailDataBean dataBean) {
                        //  绑定过学校
                        if (dataBean.getResult().getSelectResult() == 1) {
                            schoolBind.setVisibility(View.GONE);
                            cancelSchool.setVisibility(View.VISIBLE);
                        } else {
                            schoolBind.setVisibility(View.VISIBLE);
                            cancelSchool.setVisibility(View.GONE);
                        }
                        schoolId = dataBean.getResult().getSchoolId();
                        schoolLoction.setText(dataBean.getResult().getSchoolName());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }

                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
            }
        });
    }

    /**
     * 添加解除绑定对话框
     *
     * @param
     */
    private void deBindSchoolDialog(final long id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("是否确定解除绑定？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        deBindSchool(id);

                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).create().show();
    }


    @OnClick({R.id.cancel_school, R.id.school_bind, R.id.my_course_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel_school:
                deBindSchoolDialog(schoolId);
                break;
            case R.id.school_bind:
                Intent intent = new Intent(this, SelectCityActivity.class);
                startActivity(intent);
                break;

            case R.id.my_course_next:
                Intent intent1 = new Intent(this, MallCommonH5Activity.class);
                intent1.putExtra(MallCommonH5Activity.URL_KEY, Constant.getMyLesson());
                startActivity(intent1);
                break;

        }
    }


    private void deBindSchool(long school) {
        HttpMehtod.getInstance().deBindSchool(school, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
//                //解析数据
                BaseBean<DeBindSchoolDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), DeBindSchoolDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<DeBindSchoolDataBean>() {
                    @Override
                    public void success(DeBindSchoolDataBean dataBean) {

                        if (dataBean.getDeBindingResult() == 1) {
                            ToastUtil.show("解除绑成功");
                            cancelSchool.setVisibility(View.GONE);
                            schoolBind.setVisibility(View.VISIBLE);
                            schoolLoction.setText("尚未绑定任何学校");
                        } else {
                            ToastUtil.show("解除绑定失败");
                        }

                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
            }
        });
    }
}
